package com.farvin.gis;

import java.lang.reflect.Field;
import java.util.HashMap;
import java.util.Map;

import com.farvin.gis.bean.request.LoginRequestBean;

public class TestClass {
	public static void main(String[] args) {
		test1();
	}
	
	private static void test1(){
		LoginRequestBean bean = new LoginRequestBean();
		Map<String,String> valMap = new HashMap<String,String>();
		valMap.put("id", "111");
		valMap.put("method", "login");
		for(Class<?> clazz = bean.getClass();clazz!=Object.class;clazz = clazz.getSuperclass()){
			Field[] fields = clazz.getDeclaredFields();
			for(Field field : fields){
				field.setAccessible(true);
				Class<?> fieldType = field.getType();
				System.out.println(field.getName()+"==="+fieldType.isPrimitive()+"==="+fieldType);
				String val = valMap.get(field.getName());
				try{
					setFieldVal(bean,val,field);
				} catch (IllegalArgumentException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				} catch (IllegalAccessException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}
		}
		//System.out.println(bean.getUserid()+"===="+bean.getMethod());
	}
	
	private static void setFieldVal(Object bean,String strVal,Field field) throws NumberFormatException, IllegalArgumentException, IllegalAccessException{
		Class<?> fieldType = field.getType();
		if(fieldType.equals(String.class)){
			field.set(bean, strVal);
			return;
		}
		if(fieldType.equals(int.class) || fieldType.equals(Integer.class)){
			field.setInt(bean, Integer.parseInt(strVal));
			return;
		}
		if(fieldType.equals(long.class) || fieldType.equals(Long.class)){
			field.setLong(bean, Long.parseLong(strVal));
			return;
		}
		if(fieldType.equals(double.class) || fieldType.equals(Double.class)){
			field.setDouble(bean, Double.parseDouble(strVal));
			return;
		}
		if(fieldType.equals(short.class) || fieldType.equals(Short.class)){
			field.setShort(bean, Short.parseShort(strVal));
			return;
		}
		if(fieldType.equals(float.class) || fieldType.equals(Float.class)){
			field.setFloat(bean, Float.parseFloat(strVal));
			return;
		}
		if(fieldType.equals(byte.class) || fieldType.equals(Byte.class)){
			field.setByte(bean, Byte.parseByte(strVal));
			return;
		}
	}
}
